package merchant.space;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;



public class MyCollectionsActivity extends Activity {
	
	private Navigation navigation;
	private MyCollectionsAdapter adapter;
	private TextView collections_label,collections,messages_label,messages;
	private LinearLayout block2;
	private ListView list;
	
	private static final int LOADING = 0;
	private ProgressDialog loadingdialog = null;
	
	private Collections mycollections;
	public int pos;
	private MyCollectionsActivity context;
	
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.mycollectionsmain);
    	context=this;
    	
    	collections = (TextView) findViewById(R.id.collections);
    	collections_label = (TextView) findViewById(R.id.collections_label);
    	messages = (TextView) findViewById(R.id.messages);
    	messages_label = (TextView) findViewById(R.id.messages_label);
    	block2 = (LinearLayout) findViewById(R.id.second_block);
    	
    	list= (ListView) findViewById(R.id.list);
    	list.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
	        	context.pos=position;
	        	showDialog(LOADING);
	    		new Thread(){ public void run() {
	    			DataLink.prepareMyCollection(pos);
	    			threadHandler.sendEmptyMessage(0);
	    			  }}.start();
	        }});
	    		

    	MyCollectionsGroupActivity parent = (MyCollectionsGroupActivity)getParent();
    	navigation = parent.getNavigation();
    	mycollections=DataLink.getMyCollections();
    	refresh();
	   
    }
    
    public Navigation getNavigation()
    { 
    	return navigation; 
    }
    
  
    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event)
    {
    	Log.d("BACKING","MLYCOL");
    	
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
        	Log.d("BACKING","b1");
        	finish();
        }
        
        return super.onKeyDown(keyCode, event);
    }
   
    
    public void onActivityResult()
    {
    
    	Log.d("RESULT","MYCOLL");
     }
   
    
    public void viewCollection()
	{
    	Intent viewcoll = new Intent(getParent(), MyViewCollectionActivity.class);
		MyCollectionsGroupActivity parentActivity = (MyCollectionsGroupActivity) getParent();
		parentActivity.startChildActivity("MyViewCollectionActivity", viewcoll);
	}
    
  

    
    public boolean onCreateOptionsMenu(Menu menu) {
    	  Log.d("MY COLL MENU","m");
		  menu.add(0,0,0,"Add Collection");
		  return true;
	    }
    
    public void refresh()
    {
    //	setContentView(R.layout.mycollectionsmain);
    	Log.d("REFRESHING","SHING");
    	
    	if (navigation.getStatus()==Navigation.OFFLINE)
    	{
    		collections_label.setText("Please, login to get your data");
    		collections_label.setVisibility(View.VISIBLE);
    		
    		collections.setVisibility(View.GONE);
    		block2.setVisibility(View.GONE);
    		messages.setVisibility(View.GONE);
    		messages_label.setVisibility(View.GONE);
    	}
    	
    	else
    	{
    		collections_label.setText("Collections:");
    		messages_label.setText("Messages");
    		collections.setText(String.valueOf(mycollections.size()));
    		messages.setText(String.valueOf(0));
    		
    		collections.setVisibility(View.VISIBLE);
    		collections_label.setVisibility(View.VISIBLE);
    		block2.setVisibility(View.VISIBLE);
    		messages.setVisibility(View.VISIBLE);
    		messages_label.setVisibility(View.VISIBLE);
    		
    		
    		adapter = new MyCollectionsAdapter(this,navigation);
    		list.setAdapter(adapter);
    	//	String []array = new String[]{ "um","dois ","ssum","dosis ","bghjum","dasasois ","bghjum","dasasois "};
    	//	adapter = new ArrayAdapter<String>(this, R.layout.browse_cattegories,R.id.text, array);
    	//	list.setAdapter(new ArrayAdapter<String>(this, R.layout.browse_cattegories,R.id.text, array));
    		setListViewHeightBasedOnChildren(list);


    	}
    	
    	
    }
    
	    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	    //	  super.onOptionsItemSelected(item);
	    	  switch (item.getItemId()) {
	    	  case 0:
	    		  
	    		  Intent viewcoll = new Intent(getParent(), AddCollectionActivity.class);
	    		  MyCollectionsGroupActivity parentActivity = (MyCollectionsGroupActivity) getParent();
	    		  parentActivity.startChildActivity("AddCollectionActivity", viewcoll);
	    		  
	    	      break;
	    	 
	    	  
	    	  default:
//	    	          view.setText("Nothing found.");
	    	  break;
	    	  }
	    	  return false;
	    }

	   
		
			@Override
		    public void onResume() {
		    	//super.onResume();
			 super.onResume();
		    	
		    	Log.d("RESUMING","MYCOLL");
		    	if(navigation.myCollectionsRefresh())
		    	{
		    		Log.d("RESUMING","REFRESH");
		    		mycollections=DataLink.getMyCollections();
		    		
		    	//	DataLink.getCollectionsByUser(navigation.)

		    		refresh();
		    		navigation.setCollectionsRefresh(false);
		    	}
		    	
			}
			
			private final Handler threadHandler = new Handler() {
				@Override
				public void handleMessage(Message arg0) {
					switch(arg0.what)
					{
						
						case 0:
						{
							Log.d("DISMISSED","ault");
							dismissDialog(LOADING);
							viewCollection();
						} break;
						
						default:
						{
							Log.d("DEF","ault");
						} break;
					}
				}};
				
				
				@Override
				protected Dialog onCreateDialog(int id) {
					switch (id) {
					case LOADING:
						loadingdialog = new ProgressDialog(getParent());
						loadingdialog.setTitle("Merchant");
						loadingdialog.setMessage("Loading data");
						return loadingdialog;
						
						
					default:
						return null;
					}
				}
				
				public Collections getCollections() { return mycollections; }
				
				 public void setListViewHeightBasedOnChildren(ListView listView) {
				     

				        int totalHeight = 0;
				        for (int i = 0; i < adapter.getCount(); i++) {
				            View listItem = adapter.getView(i, null, listView);
				            if (listItem==null) Log.d("pos","pois");
				            Log.d("pos",listItem.toString());
				            listItem.measure(0, 0);
				            totalHeight += listItem.getMeasuredHeight();
				        }

				        ViewGroup.LayoutParams params = listView.getLayoutParams();
				        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
				        listView.setLayoutParams(params);
				    }
				}


		
		
		
